<?php
namespace App\libs\alipay\pagepay\service;

use App\libs\alipay\aop\AopClient;
use App\libs\alipay\aop\request\AlipayTradePagePayRequest;

class AlipayTradeService {

//支付宝网关地址
public $gateway_url = "https://openapi.alipaydev.com/gateway.do";

//支付宝公钥
public $alipay_public_key;

//商户私钥
public $private_key;

//应用id
public $appid;

//编码格式
public $charset = "UTF-8";

public $token = NULL;

//返回数据格式
public $format = "json";

//签名方式
public $signtype = "RSA";

function __construct(){
$this->gateway_url = config('alipay.gatewayUrl'); //获得config文件夹下的alipay.php中的gatewayUrl参数，下同。
$this->appid = config('alipay.app_id');
$this->private_key = config('alipay.merchant_private_key');
$this->alipay_public_key = config('alipay.alipay_public_key');
$this->charset = config('alipay.charset');
$this->signtype= config('alipay.sign_type');

if(empty($this->appid)||trim($this->appid)==""){
throw new Exception("appid should not be NULL!");
}
if(empty($this->private_key)||trim($this->private_key)==""){
throw new Exception("private_key should not be NULL!");
}
if(empty($this->alipay_public_key)||trim($this->alipay_public_key)==""){
throw new Exception("alipay_public_key should not be NULL!");
}
if(empty($this->charset)||trim($this->charset)==""){
throw new Exception("charset should not be NULL!");
}
if(empty($this->gateway_url)||trim($this->gateway_url)==""){
throw new Exception("gateway_url should not be NULL!");
}

}
function AlipayWapPayService($alipay_config) {
$this->__construct($alipay_config);
}

/**
* alipay.trade.wap.pay
* @param $builder 业务参数，使用buildmodel中的对象生成。
* @param $return_url 同步跳转地址，公网可访问
* @param $notify_url 异步通知地址，公网可以访问
* @return $response 支付宝返回的信息
*/
function pagePay($builder,$return_url,$notify_url) {

$biz_content=$builder->getBizContent();
//打印业务参数
$this->writeLog($biz_content);

$request = new AlipayTradePagePayRequest();

$request->setNotifyUrl($notify_url);
$request->setReturnUrl($return_url);
$request->setBizContent ( $biz_content );

// 首先调用支付api
$response = $this->aopclientRequestExecute ($request,true);
// $response = $response->alipay_trade_wap_pay_response;
return $response;
}

function aopclientRequestExecute($request,$ispage=false) {

$aop = new AopClient ();
$aop->gatewayUrl = $this->gateway_url;
$aop->appId = $this->appid;
$aop->rsaPrivateKey =  $this->private_key;
$aop->alipayrsaPublicKey = $this->alipay_public_key;
$aop->apiVersion ="1.0";
$aop->postCharset = $this->charset;
$aop->format= $this->format;
$aop->signType=$this->signtype;
// 开启页面信息输出
$aop->debugInfo=true;
if($ispage)
{
$result = $aop->pageExecute($request,"post");
echo $result;
}
else
{
$result = $aop->Execute($request);
}

//打开后，将报文写入log文件
$this->writeLog("response: ".var_export($result,true));
return $result;
}
//请确保项目文件有可写权限，不然打印不了日志。
function writeLog($text) {
// $text=iconv("GBK", "UTF-8//IGNORE", $text);
//$text = characet ( $text );
file_put_contents ( dirname ( __FILE__ ).DIRECTORY_SEPARATOR."./../../log.txt", date ( "Y-m-d H:i:s" ) . "  " . $text . "\r\n", FILE_APPEND );
}
}

?>